
#include "StdAfx.h"
#include "myapp.h"
#include ".\framewnd.h"

extern HINSTANCE hInst;

MSGMAP_ENTRY _messageEntriesFrame[] = 
{
	WM_SETFOCUS,OnSetFocus,
	WM_COMMAND,OnCommand,
	WM_DESTROY,OnDestroy,
};

CMDMAP_ENTRY _commandEntriesFrame[] = 
{
	IDM_EXIT,OnExit,
	IDM_ABOUT,OnAbout,
};

extern HWND hMainWnd;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	for(int i=0;i<dim(_messageEntriesFrame);i++)
	{
		if(message == _messageEntriesFrame[i].msg)
		{
			return (*_messageEntriesFrame[i].pfn)(hWnd,wParam,lParam);
		}
	}

	return DefWindowProc(hWnd,message,wParam,lParam);
}

LRESULT OnCommand(HWND hWnd,WPARAM wParam,LPARAM lParam)
{
	for(int i=0;i<dim(_commandEntriesFrame);i++)
	{
		if(LOWORD(wParam) == _commandEntriesFrame[i].id)
		{
			return (*_commandEntriesFrame[i].pfn)(hWnd,LOWORD(wParam),HIWORD(wParam),(HWND)lParam);
		}
	}

	return DefWindowProc(hWnd,WM_COMMAND,wParam,lParam);
}

LRESULT OnSetFocus(HWND,WPARAM,LPARAM)
{
	SetFocus(hMainWnd);
	return 0;
} 

LRESULT OnDestroy(HWND,WPARAM,LPARAM)
{
	PostQuitMessage(0);
	return 0;
}

LRESULT OnExit(HWND hWnd,UINT,UINT,HWND)
{
	DestroyWindow(hWnd);
	return 0;
}

LRESULT OnAbout(HWND hWnd,UINT,UINT,HWND)
{
	DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
	return 0;
}

LRESULT OnPaint(HWND hWnd,WPARAM,LPARAM)
{
	PAINTSTRUCT ps;
	HDC hDC = BeginPaint(hWnd,&ps);
	EndPaint(hWnd,&ps);

	return 0;
}

LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:
		return TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
		{
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		break;
	}
	return FALSE;
}
